//
//  LEEWelcomeController.swift
//  SinaWeibo
//
//  Created by 李江波 on 2016/11/15.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

private let margin : CGFloat = 120.0

class LEEWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        setupUI()
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    
 
    ///一定要在viewDidLayoutSubviews 之后调用动画，否者frame 都为0
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        setAnimation()
    }
    
    /// 设置动画
    private func setAnimation() {
        
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 7, options: .curveEaseIn, animations: {
            
            let newDistance = -(UIScreen.main.bounds.height - self.icon.bounds.size.height - margin)
            self.icon.snp.updateConstraints({ (make) in
                
                make.bottom.equalTo(self.view).offset(newDistance)
            })
            
            self.view.layoutIfNeeded()
            
        }) { (_) in
           
            UIView.animate(withDuration: 0.8, animations: {
                self.welcomeTitle.alpha = 1
                
            }){ (_) in
            
                //发送通知
                NotificationCenter.default.post(name: NSNotification.Name(ChangeRootViewController), object: "tabVc")
               
            }
            
        }
    }
    
    private func setupUI() {
        
        view.addSubview(icon)
        view.addSubview(welcomeTitle)
        
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        
        icon.sd_setImage(with: LEEOAuthViewModel.shared.iconUrl)
        
        welcomeTitle.alpha = 0
        
        icon.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-margin)
            make.size.equalTo(CGSize(width: 85, height: 85))
            
        }
        
        welcomeTitle.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(icon)
            make.top.equalTo(icon.snp.bottom).offset(50)
        }
        
    }
    
    lazy var icon = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    lazy var welcomeTitle = UILabel(title: "欢迎归来", textColor: UIColor.purple, font: 16)

}
